<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="searchForm">
				<el-form-item>
					<el-input v-model="searchForm.name" placeholder="宠物姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getUsers">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pets" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;" :stripe="true">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index">
			</el-table-column>
			<el-table-column prop="name" label="宠物姓名" align="center">
			</el-table-column>
			<el-table-column prop="state" label="宠物状态" align="center">
				<template scope="scope">
					<span v-if="scope.row.state == 0" style="color:#0c80ba">待上架
					</span>
					<span v-if="scope.row.state == -1" style="color:gray">已售出
					</span>
					<span v-if="scope.row.state == 1" style="color: chartreuse">待领养
					</span>
				</template>
			</el-table-column>

			<el-table-column prop="costprice" label="收购价格" align="center">
			</el-table-column>

			<el-table-column prop="age" label="宠物年龄" align="center">
			</el-table-column>

			<el-table-column prop="gender" label="性别" align="center">
				<template scope="scope">
					<span v-if="scope.row.gender == 1" style="color: blue">公</span>
					<span v-if="scope.row.gender == 0" style="color: red">母</span>
				</template>
			</el-table-column>

			<el-table-column prop="coat_color" label="毛色" align="center">
			</el-table-column>

			<el-table-column prop="shop.name" label="收购店铺" align="center">
			</el-table-column>

			<el-table-column label="操作" >
				<template scope="scope">
					<el-button type="primary" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增编辑公共界面-->
		<el-dialog :title="title" v-model="UserFormVisible" :close-on-click-modal="false" v-loading="UserLoading" >
			<el-form :model="UserForm" label-width="80px" :rules="UserFormRules" ref="UserForm">
				<el-form-item label="宠物昵称" prop="username">
					<el-input v-model="UserForm.username" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="资源" prop="resources">
					<el-upload
							class="upload-demo"
							action="/api/fastdfs/upload"
							:on-remove="handleRemove"
							:on-success="handleSuccess"
							:file-list="fileList"
							list-type="picture">
						<el-button size="small" type="primary">点击上传</el-button>
						<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
					</el-upload>
				</el-form-item>
				<el-form-item label="简介" prop="intro">
					<quill-editor v-model="UserForm.detail.intro"  :options="quillOption">
					</quill-editor>
				</el-form-item>
				<el-form-item label="预约须知" prop="orderNotice" >
					<quill-editor v-model="UserForm.detail.orderNotice" :options="quillOption" >
					</quill-editor>
				</el-form-item>

				<el-form-item label="宠物状态">
					<el-radio-group v-model="UserForm.state">
						<el-radio class="radio" :label = 0 >待上架</el-radio>
						<el-radio class="radio" :label = 1 >待领养</el-radio>
						<el-radio class="radio" :label = -1 >已售出</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer" align="center">
				<el-button type="primary" @click.native="UserSubmit" :loading="UserLoading">编辑提交</el-button>
				<el-button type="primary" @click.native="on_shelf" :loading="UserLoading" :style="{ display: onshelfdisplay}">宠物上架</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';
	import { quillEditor } from "vue-quill-editor"; //调用编辑器
	import 'quill/dist/quill.core.css';
	import 'quill/dist/quill.snow.css';
	import 'quill/dist/quill.bubble.css';
	import quillConfig from '../../common/js/quill-config.js'
	export default {
		components: {
			quillEditor
		},
		data() {
			return {
                searchForm: {
					name: ''
				},
				total: 0,
				page: 1,
				listLoading: false,
				sels: [],//列表选中列

				UserFormVisible: false,//编辑界面是否显示
				UserLoading: false,
				onshelfdisplay:'',//上架按钮默认关闭
				UserFormRules: {

				},
				//新增编辑界面数据
				UserForm: {
					id:null,
					name: '',
					state:'',
					age: '',
					gender:'',
					coat_color:'',
					resources:[],
					saleprice:'',
					offsaletime:'',
					onsaletime:'',
					costprice:'',
					createtime:'',
					detail:{
						intro:'',
						orderNotice:''
					}
				},
				title:'',
				quillOption: quillConfig,
				fileList:[],//上传的文件列表
			}
		},
		methods: {
			handleCurrentChange(val) {//获取所在页码
				this.page = val;
				this.getUsers();//点击页码时调用钩子方法
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			/**显示编辑界面*/
			handleEdit: function (index, row) {
				this.UserFormVisible = true;
				this.title='编辑';
				this.UserForm = Object.assign({}, row);
				//图片回显
				this.fileList = [];
				var resources =  row.resources;
				var resourcesArr = resources.split(",");
				for(var i=0;i<resourcesArr.length;i++){
					console.log(resourcesArr[i]);
					this.fileList.push({url:resourcesArr[i]})
				}
				if (this.UserForm.state!=0){
					this.onshelfdisplay='none';
				}
			},
			/**显示新增界面*/
			handleAdd: function () {
				this.onshelfdisplay='none';
				this.UserFormVisible = true;
				this.title='新增';
				this.UserForm = {
					id:null,
					name: '',
					state:'',
					age: '',
					gender:'',
					coat_color:'',
					resources:[],
					saleprice:'',
					offsaletime:'',
					onsaletime:'',
					costprice:'',
					createtime:'',
					detail:{
						intro:'',
						orderNotice:''
					}
				};
			},
			/**获取用户列表*/
			getUsers() {
			    //console.debug(this.searchForm)
				let para = {
					currentPage: this.page,
					keywords:this.searchForm.name
				};
				//打开加载转圈圈的效果
				this.listLoading = true;
				//查询数据
				this.$http.post("/pet",para).then(res=>{
				    this.pets = res.data.data;
				    this.total=res.data.total;
                    //关闭加载的效果
                    this.listLoading = false;
				});
			},

			/**删除*/
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { id: row.id };
					this.$http.delete("/pet/"+para.id).then((res) => {
						this.listLoading = false;
						if (res.data.success){
							this.$message({
								message: '删除成功',
								type: 'success'
							});
						}else {
							this.$message({
								message: "删除失败"+res.data.message,
								type: 'warning'
							});
						}
						this.getUsers();
					});
				}).catch(() => {
				});
			},
			/**批量删除*/
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				var names = this.sels.map(item => item.name).toString();
				this.$confirm('确认删除选中记录 '+ names +' 吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					console.debug(ids);
					let para = { ids: ids };
					this.$http.post('/pet/dels',ids).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						if (res.data.success){
							this.$message({
								message: '删除成功',
								type: 'success'
							});
						}else {
							this.$message({
								message: "删除失败"+res.data.message,
								type: 'warning'
							});
						}
						this.getUsers();
					});
				}).catch(() => {
				});
			},
			/**上架*/
			on_shelf:function(){
				//验证当前的表单是否符合验证规则
				this.$refs.UserForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认上架吗？', '提示', {}).then(() => {
							this.UserLoading = true;
							//把表单的数据组装为json
							let para = Object.assign({}, this.UserForm);
							this.$http.put('/pet/on_shelf',para).then((res) => {
								this.UserLoading = false;
								if (res.data.success){
									this.$message({
										message: '成功',
										type: 'success'
									});
								}else {
									this.$message({
										message: "失败"+res.data.message,
										type: 'warning'
									});
								}
								//重置表单-调用现成的方法 清空数据
								this.$refs['UserForm'].resetFields();
								//关闭 更新界面
								this.UserFormVisible = false;
								//重新刷新页面数据 钩子方法
								this.getUsers();
							});
						});
					}
				});
			},
			/**编辑和新增提交*/
			UserSubmit: function () {
				//验证当前的表单是否符合验证规则
				this.$refs.UserForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.UserLoading = true;
							//把表单的数据组装为json
							let para = Object.assign({}, this.UserForm);
							this.$http.put('/pet',para).then((res) => {
								this.UserLoading = false;
								if (res.data.success){
									this.$message({
										message: '提交成功',
										type: 'success'
									});
								}else {
									this.$message({
										message: "更新失败"+res.data.message,
										type: 'warning'
									});
								}
								//重置表单-调用现成的方法 清空数据
								this.$refs['UserForm'].resetFields();
								//关闭 更新界面
								this.UserFormVisible = false;
								//重新刷新页面数据 钩子方法
								this.getUsers();
							});
						});
					}
				});
			},
		},
		mounted() {
		    //当页面加载完以后。执行的方法
			this.getUsers();
		}
	}
</script>

<style scoped>

</style>